$(document).ready(function(){
    emailReg = /^[\w._-]+@[\w.-]+\.[A-Za-z]{2,4}$/;
    special_char = "*|,\":<>[]{}`\';()@&$#%_+\\~?!^=./-";
    
    /*
    username = $("#username").val();
    password = $("#password").val();
    confirm_password = $("#confirm_password").val();
    fullname = $("#fullname").val();
    email = $("#email").val();
    birthday = $("#mask-date").val();
    phone = $("#phone").val();
    */
   
    $("#save").click(function(){
        hide_error();
        username = $("#username").val();
        if(username ==''){
            $("#username_err_null").show();
            $("#username").focus();
            return false;
        }
        else{
            len = username.length;
            for (var i = 0; i < len; i++) {
                if (special_char.indexOf(username.charAt(i)) != -1){
                    $("#username_err_special").show();
                    $("#username").focus();
                    return false;
                }
            }
            dataString = 'username='+username;
            $.ajax({
                type: "POST",
                url: "?r=systemAccount/create",
                data: dataString,
                success: function(data){
                    patt = /true/g;
                    if(patt.test(data)){
                        $("#username_ok").show();
                    }else{
                        $("#username_err_dup").show();
                        $("#username").focus();
                        return false;
                    }
                }
            });
        }
        
        password = $("#password").val();
        confirm_password = $("#confirm_password").val();
        
        if(password.length < 6){
            $("#password_err").show();
            $("#password").focus();
            return false;
        }
        if(confirm_password != password){
            $("#confirm_password_err").show();
            $("#confirm_password").focus();
            return false;
        }
        
        fullname = $("#fullname").val(); 
        if(fullname ==''){
            $("#fullname_err").show();
            $("#fullname").focus();
            return false;
        }
        
        email = $("#email").val();
        
        if(!emailReg.test(email)){
            $("#email_err").show();
            $("#email").focus();
            return false;
        }else{
            dataString = 'email='+email;
            $.ajax({
                type: "POST",
                url: "?r=systemAccount/create",
                data: dataString,
                success: function(data){
                    patt = /false/g;
                    if(patt.test(data)){
                        $("#email_err_dup").show();
                        $("#email").focus();
                        return false;
                    }
                }
            });
        }
        
        birthday = $("#mask-date").val();
        if(birthday ==''){
            $("#birthday_err").show();
            $("#mask-date").focus();
            return false;
        }
        
        phone = $("#phone").val();
        if(isNaN(phone) == true){
            $("#phone_err").show();
            $("#phone").focus();
            return false;
        }
        
        $("#create_form").submit();
    });
    
    $("#email").blur(function(){
        hide_error();
        email = $("#email").val();
        
        if(!emailReg.test(email)){
            $("#email_err").show();
            $("#email").focus();
            return false;
        }else{
            dataString = 'email='+email;
            $.ajax({
                type: "POST",
                url: "?r=systemAccount/create",
                data: dataString,
                success: function(data){
                    patt = /false/g;
                    if(patt.test(data)){
                        $("#email_err_dup").show();
                        $("#email").focus();
                        return false;
                    }
                }
            });
        }
        
    });
    
    $("#confirm_password").blur(function(){
        hide_error();
        password = $("#password").val();
        confirm_password = $("#confirm_password").val();
        
        if(password.length < 6){
            $("#password_err").show();
            $("#password").focus();
            return false;
        }
        if(confirm_password != password){
            $("#confirm_password_err").show();
            $("#confirm_password").focus();
            return false;
        }
    });
    
    $("#username").blur(function(){
        hide_error();  
        username = $("#username").val();
        if(username ==''){
            $("#username_err_null").show();
            $("#username").focus();
            return false;
        }
        else{
            len = username.length;
            for (var i = 0; i < len; i++) {
                if (special_char.indexOf(username.charAt(i)) != -1){
                    $("#username_err_special").show();
                    $("#username").focus();
                    return false;
                }
            }
            dataString = 'username='+username;
            $.ajax({
                type: "POST",
                url: "?r=systemAccount/create",
                data: dataString,
                success: function(data){
                    patt = /true/g;
                    if(patt.test(data)){
                        $("#username_ok").show();
                    }else{
                        $("#username_err_dup").show();
                        $("#username").focus();
                        return false;
                    }
                }
            });
        }
        
    });
});

function hide_error(){
    $("#username_err_null").hide();
    $("#username_err_dup").hide();
    $("#username_err_special").hide();
    $("#username_ok").hide();
    $("#password_err").hide();
    $("#confirm_password_err").hide();
    $("#fullname_err").hide();
    $("#birthday_err").hide();
    $("#email_err").hide();
    $("#email_err_dup").hide();
    $("#phone_err").hide();
}